package org.kolonitsky.css;

import org.mockito.ArgumentMatcher;
import org.w3c.css.sac.LexicalUnit;

/**
 * @author Alex.Kolonitsky
 */
public class LexicalUnitMather extends ArgumentMatcher<LexicalUnit> {

    private final LexicalUnit wanted;

    public static LexicalUnitMather eq(LexicalUnit lu) {
        return new LexicalUnitMather(lu);
    }

    protected LexicalUnitMather(LexicalUnit wanted) {
        this.wanted = wanted;
    }

    public boolean matches(LexicalUnit lu1, LexicalUnit lu2) {
        return (lu1 != null || lu2 == null)
            && (lu1 == null || lu2 != null)
            && (lu1 == null || lu1.getStringValue().equals(lu2.getStringValue())
                            && matches(lu1.getNextLexicalUnit(), lu2.getNextLexicalUnit()));

    }

    @Override
    public boolean matches(Object o) {
        return o instanceof LexicalUnit
               && matches(wanted, (LexicalUnit) o);

    }
}
